/*
 * Copyright 2014 mango.jfaster.org
 *
 * The Mango Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package org.jfaster.mango.page;

import javax.annotation.Nullable;

/**
 * @author ash
 */
public class Page {

  private final int pageNum;

  private final int pageSize;

  private final Sort sort;

  private Page(int pageNum, int pageSize, Sort sort) {
    if (pageNum < 0) {
      throw new IllegalArgumentException("pageNum need >= 0, but pageNum is " + pageNum);
    }
    if (pageSize <= 0) {
      throw new IllegalArgumentException("pageSize need > 0, but pageSize is " + pageSize);
    }

    this.pageNum = pageNum;
    this.pageSize = pageSize;
    this.sort = sort;
  }

  public static Page of(int pageNum, int pageSize) {
    return of(pageNum, pageSize, null);
  }

  public static Page of(int pageNum, int pageSize, Direction direction, String... properties) {
    return of(pageNum, pageSize, Sort.by(direction, properties));
  }

  public static Page of(int pageNum, int pageSize, Sort sort) {
    return new Page(pageNum, pageSize, sort);
  }

  public int getPageNum() {
    return pageNum;
  }

  public int getPageSize() {
    return pageSize;
  }

  @Nullable
  public Sort getSort() {
    return sort;
  }

}
